package com.remon.quartz.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转cron表达式
 *
 * @author remon
 * @create 2018-08-05 0:55
 **/
public class Date2CornUtil {
    private static final Logger logger = LoggerFactory.getLogger(Date2CornUtil.class);

    /***
     *
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateByPattern(Date date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /***
     * convert Date to cron ,eg.  "0 06 10 15 1 ? 2014"
     * @param date  : 时间点
     * @return
     */
    public static String getCron(java.util.Date date) {
        String dateFormat = "ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }

    /***
     * convert Date to cron ,eg.  "0 06 10 15 1 ? 2014"
     * @param dateStr  : 时间点
     * @return
     */
    public static String getCron(String dateStr) throws ParseException {
        String dateFormat = "ss mm HH dd MM ? yyyy";

        return formatDateByPattern(getNowDate(dateStr), dateFormat);
    }

    public static Date getNowDate(String dateStr) throws ParseException {
//        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String dateString = formatter.format(currentTime);
        Date currentTime_2 = formatter.parse(dateStr);
        return currentTime_2;
    }
}
